<?php
require_once 'config.php';

$db_host = DB_HOST;
$db_user = DB_USER;
$db_pw = DB_PWD;
$db_name = DB_PREFIX . "_" . DB_NAME;
$link = mysql_connect($db_host,$db_user,$db_pw);
if(!$link){
   echo "Can't connect to mysql server.";
   exit();
}
mysql_query("USE `$db_name`");

$sql = "select id from product_items where is_delete = 0 ";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result,MYSQL_NUM)){
     $id = $row[0];
     if( $id > 0){
     	
     	//set null 
        $sql4 = "update product_items set default_item_image_id = null where id = $id";
        mysql_query($sql4);
        //set default image id     	
        
     	$sql2 = "select id from product_images where is_delete=0 and default_item_image = 1 and item_id = $id";
     	$result2 = mysql_query($sql2);
        while($row2 = mysql_fetch_array($result2,MYSQL_NUM)){
        	$imageId = $row2[0];
        	if(!empty($imageId) && $imageId > 0){
        		$sql3 = "update product_items set default_item_image_id = $imageId where id = $id";
        		mysql_query($sql3);
        	}       	
        }
     }
}

mysql_close($link);